package cn.edu.hit.service;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import cn.edu.hit.db.DBTools;
import cn.edu.hit.javabean.*;

public class RoomBedService {
	public static int HAS_SELECT_BEFORE = 0;
	public static int SELECT_SUCCESS = 1;
	public static int SELECT_FAIL = 2;
	public static int FULL = 3;
	public static DBTools dbtools = new DBTools();
	public String hello()
	{
		return "hello";
	}
	public List<Bed> getBedsByRoomNumber(String roomNumber)
	{
		ArrayList<Bed> beds = new ArrayList<Bed>();
		String sql = "select * from bed where room_number = '"+roomNumber+"'";
		System.out.println(sql);
		ResultSet rs = dbtools.executeQuery(sql);
		try {
			Bed bed = null;
			while(rs.next())
			{
				int position = rs.getInt("bed_position_id");
				int isChoosen = rs.getInt("isChoosen");
				String aroomNumber  =rs.getString("room_number");
				bed = new Bed(position,aroomNumber,isChoosen);
				System.out.println(position+":"+isChoosen);
			    beds.add(bed);
			}
			return beds;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return beds;
	}
	public static int getTypeByRoomNumber(String roomNumber)
	{
		String sql = "select type from room where room_number = '"+roomNumber+"'";
		ResultSet rs = dbtools.executeQuery(sql);
		try {
			if(rs.next())
			{
				int type =rs.getInt("type");
				return type;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return -1;
	}
	public static int selectRoomBed(String roomBed,String studentID)
	{
		String sql = "select * from student_room where student_number = '"+studentID+"'";
		ResultSet rs = dbtools.executeQuery(sql);
		try {
			if(rs.next())
				return RoomBedService.HAS_SELECT_BEFORE;
			String roomNumber = roomBed.substring(0, roomBed.length()-1);
			String bedPosition = roomBed.substring(roomBed.length()-1);
			sql = "select total,current_number from room where room_number = '"+roomNumber+"'";
			System.out.println(sql);
			rs = dbtools.executeQuery(sql);
			if(rs.next())
			{
				if(rs.getInt("total") == rs.getInt("current_number"))
					return RoomBedService.FULL;
			}
			sql = "insert into student_room values('"+studentID+"',"+Integer.parseInt(bedPosition)+",'"+roomNumber+"')";
			System.out.println(sql);
			dbtools.executeUpdate(sql);
			int currentNumber = rs.getInt("current_number")+1;
			sql = "update room set current_number = "+currentNumber+" where room_number ='"+roomNumber+"'";
			System.out.println(sql);
			dbtools.executeUpdate(sql);
			sql = "update bed set isChoosen = 1 " + "where room_number='"+roomNumber+"' and bed_position_id = "+ bedPosition+"";
			System.out.println(sql);
			dbtools.executeUpdate(sql);
			return RoomBedService.SELECT_SUCCESS;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
		
		return 0;
	}
}
